﻿using AutoMapper;
using Common.MyCustomException;
using IService;
using Model.Dto.AddressBook;
using Model.Entitys;
using SqlSugar;

namespace Service
{
    public class AddressBookService : IAddressBookService
    {
        private readonly ISqlSugarClient _db;
        private readonly IMapper _mapper;
        public AddressBookService(ISqlSugarClient db, IMapper mapper)
        {
            _db = db;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="addressBookAdd"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> SaveAsync(AddressBookAdd addressBookAdd)
        {
            var addressBook = _mapper.Map<AddressBook>(addressBookAdd);
            addressBook.CreateUser = (long)addressBookAdd.UserId;
            addressBook.UpdateUser = (long)addressBookAdd.UserId;
            var col= await _db.Insertable(addressBook)
                .ExecuteReturnSnowflakeIdAsync();
            if(col>0) return true;
            throw new MyCustomException("操作失败");
        }
        /// <summary>
        /// 设置默认地址
        /// </summary>
        /// <param name="addressBookEdit"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task SetDefaultAsync(AddressBookEdit addressBookEdit)
        {
            var addressBook = _mapper.Map<AddressBook>(addressBookEdit);
            await _db.Updateable(addressBook)
                     .Where(it=>it.UserId==addressBookEdit.UserId)
                     .UpdateColumns(it => it.IsDefault)
                     .ExecuteCommandAsync();
            addressBook.IsDefault = 1;
            await _db.Updateable(addressBook)
                .Where(it => it.UserId == addressBookEdit.UserId&& it.Id== addressBookEdit.Id)
                .IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true)
                .ExecuteCommandAsync();
        }
        /// <summary>
        /// 根据id查询地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AddressBook> GetAddressBookByIdAsync(long id)
        {
            return await _db.Queryable<AddressBook>().FirstAsync(it => it.Id == id);
        }
        /// <summary>
        /// 查询默认地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AddressBook> GetDefaultAsync(long id)
        {
            return await _db.Queryable<AddressBook>()
                .FirstAsync(it => it.UserId == id&&it.IsDefault==1);
        }
        /// <summary>
        /// 查询指定用户的全部地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<AddressBook>> GetAddressBooksAsync(long id)
        {
            return await _db.Queryable<AddressBook>()
                .Where( it => it.UserId == id)
                .OrderBy(it=>it.UpdateTime, OrderByType.Desc)
                .ToListAsync();
        }
    }
}
